package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.UserInfo;
import com.gxa.p2p.pojo.Withdrawal;
import com.gxa.p2p.service.WithdrawalService;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cxy
 * Date: 2020/08/03 11:28
 * Description:
 * Version: V1.0
 */
@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/withdrawal")
@Api(value = "提现控制器", tags = "提现申请/记录")
public class WithdrawalController {

    @Resource(name = "withdrawalService")
    private WithdrawalService withdrawalService;


    @PostMapping("/selectLimitLike")
    @ApiOperation(value = "分页查和模糊查询的接口",notes = "提现审核的分页和条件显示方法",response = Map.class,httpMethod = "POST")
    public Map<String,Object> selectLimitLike(@RequestBody @ApiParam(name = "map",readOnly = true, type = "java.util.Map") Map<String,Object> map) {

        List<Withdrawal> selectList = withdrawalService.selectLimitLike(map);

        Integer count = withdrawalService.count(map);

        if(selectList.size() > 0) {
            MapUtil mapUtil = new MapUtil();
            mapUtil.put("count",count);
            mapUtil.setData(selectList);
            mapUtil.setCode(0);
            return mapUtil.getMap();
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }

    }

    @PostMapping("/selectByPhoneNumber")
    @ApiOperation(value = "条件查询的接口",notes = "查询当前用户的提现记录",response = Map.class,httpMethod = "POST")
    public Map<String,Object> selectByPhoneNumber(@RequestBody @ApiParam(name = "userInfo",readOnly = true, type = "UserInfo") UserInfo userInfo) {

        MapUtil mapUtil = new MapUtil();

        List<Withdrawal> withdrawalList = withdrawalService.selectByPhoneNumber(userInfo);
        if(withdrawalList.size() > 0) {
            mapUtil.setData(withdrawalList);
            mapUtil.setCode(0);
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     *
     * @param withdrawal
     * @return
     */

    @PutMapping
    @ApiOperation(value = "修改提现状态接口",notes = "修改提现审核通过后的状态方法",response = Map.class,httpMethod = "PUT")
    public Map<String,Object> update(@RequestBody @ApiParam(name = "withdrawal",required = true,type = "Withdrawal") Withdrawal withdrawal) {

        MapUtil mapUtil = new MapUtil();

        Integer updateCount = withdrawalService.update(withdrawal);

        withdrawalService.doWithdraw(withdrawal);

        if(updateCount > 0) {
            mapUtil.setCode(0);
        }else{
            throw new BusinessException(ErrorCode.FAILED);
        }

        return mapUtil.getMap();
    }

    /**
     * 提现申请接口,在这里验证金额以及密码是否正确,然后存到数据库里等待审核.
     * @param withdrawal 参数  需要userId 用户编号 witpassword  交易密码 withdrawalAmount提现金额 bankcardid银行卡id
     */
    @PostMapping
    @ApiOperation(value = "提现申请接口",notes = "接受提现申请创建提现订单",response = Map.class,httpMethod = "POST")
    public Map<String, Object> withdrawalRequest(@RequestBody Withdrawal withdrawal) {
        String verify = withdrawalService.verify(withdrawal);
        MapUtil mapUtil = new MapUtil();
        switch (verify) {
            case "成功":
                withdrawalService.withdrawRequest(withdrawal);
                mapUtil.setCode("0");
                mapUtil.setMsg("提现申请成功,进入审核阶段");
                break;
            case "提现金额异常":
                mapUtil.setCode("1");
                mapUtil.setMsg("提现金额错误");
                break;
            case "账户可用余额不足":
                mapUtil.setCode("1");
                mapUtil.setMsg("账户可用余额不足");
                break;
            case "密码":
                mapUtil.setCode("1");
                mapUtil.setMsg("交易密码错误");
                break;
        }
        return mapUtil.getMap();
    }

    /**
     * 提现记录全查询
     */
    @PostMapping("/selectAll")
    public Map<String,Object> findAll ()
    {
        MapUtil mapUtil = new MapUtil();
        List<Withdrawal> withdrawal = withdrawalService.selectAll();
        if (withdrawal.size()>0)
        {
            mapUtil.setCode(0);
            mapUtil.setMsg("查询成功");
            mapUtil.setData(withdrawal);
        }
        else
        {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 模糊查询接口
     * @param map 提供提现记录模糊/分页查询
     */
    @PostMapping("/selectLikeLimit")
    public Map<String,Object> LikeLimit (@RequestBody Map<String,Object> map)
    {   MapUtil mapUtil = new MapUtil();
        List<Withdrawal> withdrawal = withdrawalService.selectLikeLimit(map);
        Integer count = withdrawalService.count(map);
        if (withdrawal.size()>0)
        {   mapUtil.put("count",count);
            mapUtil.setCode(0);
            mapUtil.setMsg("查询成功");
            mapUtil.setData(withdrawal);
        }
        else
        {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

}
